#include "../include/my_msg_thread.h"
#include "../include/my_tcp_server.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

key_t key;
int msqid;

int my_msq_thread_init(pthread_t* pid){
    //获取消息队列的id
    key = ftok("root", 1);
    msqid = msgget(key, IPC_CREAT | 0664);
    //启动消息队列数据接收线程
    if(pthread_create(pid, NULL, my_msg_thread_run,NULL)!=0){
        printf("消息队列数据接收处理线程创建失败\n");
        return -1;
    }
    return 0;
}

int my_msg_thread_send(unsigned int *buf, unsigned int pointsNum){
    struct msgbuf msg;
    msg.mtype = 2;
    memcpy(msg.mtext, buf, 1000*4);
    if(msgsnd(msqid, &msg, 1000*4, 0) == 0){
        printf("向[消息队列]发送数据，成功\n");
    }else{
        printf("向[消息队列]发送数据，失败!!!\n");
    }
}

void* my_msg_thread_run(void* args){
    while(1){
        struct msgbuf msg;
        msgrcv(msqid, &msg, 1, 1, 0);
        printf("收到[消息队列]获取装置采集波形的【指令】\n");
        //通过tcp下发
        my_tcp_server_sendTakeWave();
        printf("完成[TCP]的数据下发\n");
    }

}
